/*************************************************************************** 
 * (c) 2006 Dmitry Osinovsky, osdmsoftware@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Microsoft.Win32;

namespace OsdmSoftware.PocketPicture
{
    public class UndoAction: ToolCollection
    {
        public UndoAction(ToolMenu[] menus): base("Undo", "Undo", false, menus, "Undo", null)
        {
            /*this.Name = "Undo";
            this.ImageName = "Undo";
            this.Hint = "Undo";*/
        }

        public override void DoAction()
        {
            if (Form.Mode.CanCancel())
            {
                Form.Mode.Cancel();
            }
            else
            {
                Form.DoUndo();
            }

            if (Form.ShowRedo)
            {
                base.DoAction();
            }
        }
    }

    public class RedoAction: Action
    {
        public RedoAction()
        {
            this.Name = "Redo";
            this.ImageName = "Redo";
            this.Hint = "Redo";
            this.ParentName = "Undo";
        }

        public override void DoAction()
        {
            Form.DoRedo();
            /*if (Form.Mode.CanCancel())
            {
                Form.Mode.Cancel();
            }
            else
            {
                Form.DoUndo();
            }*/

            base.DoAction();
        }
    }

    public class SetZoomAction: Action
    {
        int zoomFactor;

        public SetZoomAction(int zoomFactor)
        {
            if (zoomFactor / BitmapZoomer.ZoomFactorUnit * BitmapZoomer.ZoomFactorUnit == zoomFactor)
                this.Name = "x" + (zoomFactor/ BitmapZoomer.ZoomFactorUnit).ToString();
            else
                this.Name = zoomFactor.ToString() + "%";
            this.zoomFactor = zoomFactor;
            if (zoomFactor < 7 * BitmapZoomer.ZoomFactorUnit && zoomFactor >= BitmapZoomer.ZoomFactorUnit)
                this.ParentName = "Zoom1";
            else
                this.ParentName = "Zoom2";
            //this.CreateImageWithTextIfNoImage = false;
            this.Hint = "Zoom " + this.Name;
        }

        public override void DoAction()
        {
            Form.BitmapZoomer.ZoomFactor = zoomFactor;
            base.DoAction();
        }
    }

    public class ZoomToFitAction: Action
    {
        public ZoomToFitAction()
        {
            this.Name = "Fit";
            this.ParentName = "Zoom1";
            this.Hint = "Zoom to fit image in window";
        }

        public override void DoAction()
        {
            int zoomFactor = Math.Min((Form.ScrollBox.Width - 1) * BitmapZoomer.ZoomFactorUnit / Form.BitmapZoomer.Bitmap.Width,
                (Form.ScrollBox.Height - 1) * BitmapZoomer.ZoomFactorUnit / Form.BitmapZoomer.Bitmap.Height);
            if (zoomFactor < 1)
                zoomFactor = 1;
            Form.BitmapZoomer.ZoomFactor = zoomFactor;
            base.DoAction();
        }
    }

    public class ZoomInTool: PaintTool
    {
        public ZoomInTool()
            : base("Select zoom-in tool")
        {
            this.Name = "Zoom in";
            this.ParentName = "Zoom1";
            this.ImageName = "ZoomIn";
        }

        public override void MouseDown(int x, int y)
        {
            Form.BitmapZoomer.SetZoom(Convert.ToInt32(Form.BitmapZoomer.ZoomFactor * 1.5), x, y);
            //base.MouseDown(x, y);
        }

        public override void SelectTool()
        {
            Form.HintLabel.Text = "Point to image part to enlarge";
            base.SelectTool();
        }

        public override bool TemporaryTool()
        {
            return true;
        }

        public override void DoAction()
        {
            if (Form.Mode == this)
                Form.BitmapZoomer.ZoomFactor = Convert.ToInt32(Form.BitmapZoomer.ZoomFactor * 1.5);
            base.DoAction();
        }
    }

    public class ZoomOutAction: Action
    {
        public ZoomOutAction()
        {
            this.Name = "Zoom out";
            this.ParentName = "Zoom1";
            this.ImageName = "ZoomOut";
            this.Hint = "Zoom out";
        }

        public override void DoAction()
        {
            if (Form.Mode is ZoomInTool)
                Form.SetMode(null);
            Form.BitmapZoomer.ZoomFactor = Convert.ToInt32(Form.BitmapZoomer.ZoomFactor / 1.5);
            base.DoAction();
        }
    }

    public class ZoomMenu: ToolCollection
    {
        //int defaultZoom = 10;
        Point defaultPosition = new Point(0, 0);
        //int defaultPositionX = 0;
        //int defaultPositionY = 0;
        //int oldWidth = 0;
        //int oldHeight = 0;

        public ZoomMenu(ToolMenu[] menus, ZoomInTool zoomInTool)
            : base("Zoom", "ZoomMenu", true, menus, "Zoom menu", zoomInTool)
        {
        }

        public override void UpdatePicture(Action childAction)
        {
            if (childAction is ZoomInTool || childAction is ZoomOutAction)
            {
                base.UpdatePicture(childAction);
            }
            else
            {
                ImageIndex = OwnImageIndex;
                if (Element != null)
                {
                    Element.ImageIndex = OwnImageIndex;
                    Element.Refresh();
                }
            }
        }

        /*public override void DoAction()
        {
            Element.Panel.DisplayDropDownMenu(Element);
            CloseDropDownOnClick = false;*/

            /*bool childAction = false;
            Action defaultMenuItem;
            if (Form.DefaultMenuItems.TryGetValue(this, out defaultMenuItem))
            {
                if (defaultMenuItem is ZoomInTool || defaultMenuItem is ZoomOutAction)
                {
                    defaultMenuItem.DoAction();
                    childAction = true;
                }
            }

            if (!childAction)
            {
                if (Form.BitmapZoomer.ZoomFactor != 1)
                {
                    defaultPosition = Form.ScrollBox.Position;
                    defaultZoom = Form.BitmapZoomer.ZoomFactor;
                    oldWidth = Form.BitmapZoomer.Bitmap.Width;
                    oldHeight = Form.BitmapZoomer.Bitmap.Height;
                    Form.BitmapZoomer.ZoomFactor = 1;
                }
                else
                {
                    Form.BitmapZoomer.ZoomFactor = defaultZoom;
                    if (oldWidth == Form.BitmapZoomer.Bitmap.Width && oldHeight == Form.BitmapZoomer.Bitmap.Height)
                    {
                        Form.ScrollBox.Position = defaultPosition; //new Point(Math.Abs(defaultPosition.X), Math.Abs(defaultPosition.Y));
                    }
                }
            }

            base.DoAction();*/
        //}
    }

    public class PenWidthAction: Action
    {
        int penWidth;

        public PenWidthAction(int penWidth)
        {
            this.Name = penWidth.ToString() + " p";
            this.penWidth = penWidth;
            this.ParentName = "BrushSize";
            this.Hint = "Set brush width to " + penWidth.ToString() + " pixels";
        }

        public override void DrawImage(Graphics gr)
        {
            gr.DrawLine(new Pen(Color.Green, penWidth), 0, 0, Form.ImageList.ImageSize.Width, Form.ImageList.ImageSize.Height);
            //Form.ImageList.ImageSize.Width
            //base.DrawImage(gr);
        }

        public override void CreateUI(PocketPictureForm form)
        {
            base.CreateUI(form);
            if (form.PenWidth == penWidth)
            {
                Form.SelectedPen = this;
                SetPushed(true);
                //Form.SetDefaultAction(ParentMenu, this);
            }
        }

        public override void DoAction()
        {
            if (Form.SelectedPen != null)
                Form.SelectedPen.SetPushed(false);
            Form.PenWidth = penWidth;
            Form.SelectedPen = this;
            SetPushed(true);
            Registry.SetValue(PocketPictureForm.RegKey, "PenWidth", penWidth);

            base.DoAction();
        }
    }

    public class ViewPaletteAction: Action
    {
        public ViewPaletteAction()
        {
            this.Name = "View palette";
            this.ImageName = "ViewPalette";
            this.ParentName = "AdvancedColor";
            this.Hint = "Show palette";
        }

        public override void CreateUI(PocketPictureForm form)
        {
            base.CreateUI(form);
            SetPushed(true);//Form.PaletteControl.Visible);
        }

        public override void DoAction()
        {
            if (Form.PaletteControlDropDown)
            {
                CloseDropDownOnClick = false;
                if (Form.PaletteAsHSV)
                    Element.Panel.DisplayDropDownMenuForToolBand(Element, new HSVDropDownControl(Form, null), Form.ToolBand);
                else
                    Element.Panel.DisplayDropDownMenu(Element);
            }
            else
            {
                CloseDropDownOnClick = true;
                Form.PaletteControl.Visible = !Form.PaletteControl.Visible;
                SetPushed(Form.PaletteControl.Visible);
            }
            base.DoAction();
        }
    }

    public class ColorDialogAction: Action
    {
        public ColorDialogAction()
        {
            this.Name = "Color dialog";
            this.ImageName = "ColorDialog";
            this.ParentName = "AdvancedColor";
            this.Hint = "Enter red-green-blue values for color";
        }

        public override void DoAction()
        {
            Form.ShowColorDialog();
            base.DoAction();
        }
    }
}